﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class OrderLineApiController : ApiController
    {
        public List<OrderLine> Get()
        {
            try
            {
                return new List<OrderLine>(); 
                return new OrderLineRepository().GetList();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public List<OrderLine> Get(int? id)
        {
            try
            {
                if(id.HasValue && id>0)
                return new OrderLineRepository().GetList(id.Value);
                return new List<OrderLine>(); 
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public OrderLineRepository Get(int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                //if (!String.IsNullOrWhiteSpace(lotCode))
                //    whereClause = String.Format(whereClause + " AND lotCode.Contains(\"{0}\")", lotCode);

                return new OrderLineRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var orderlineRepository = new OrderLineRepository();
                    orderlineRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public OrderLineRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var orderlineRepository = new OrderLineRepository();
                orderlineRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public OrderLine Put(OrderLine orderline)
        {
            try
            {
                var orderlineRepository = new OrderLineRepository();
                int orderlineID = orderlineRepository.Update(orderline);

                return new OrderLine();
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public void Put(List<OrderLine> orderlines)
        {
            try
            {
                var orderlineRepository = new OrderLineRepository();
                foreach(OrderLine item in orderlines)

                orderlineRepository.Update(item);

            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }
    }
}
